﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

/// <summary>
/// All Settings for the website are handled here.
/// </summary>
public class Settings
{
	public Settings(string virtualPath)
	{
		m_path = HttpContext.Current.Server.MapPath(virtualPath);
		m_parameters = File.ReadAllLines(m_path).ToList();
	}
	protected List<string> m_parameters;
	private string m_path;
	public virtual void Clear()
	{
		m_parameters = new List<string>();
	}	
	public virtual void Save()
	{
		File.WriteAllLines(m_path,m_parameters);
	}
	public string GetParameter(string name, string defaultValue = null)
	{
		foreach (var line in m_parameters)
		{
			if (!String.IsNullOrEmpty(line))
			{
				int i = line.IndexOf('=');
				if (line.Substring(0, i).Trim() == name)
					defaultValue = line.Substring(i + 1).Trim();
			}
		}
		return defaultValue;
	}
	public class MailSettings : Settings
	{
		public MailSettings(string virtualPath) : base(virtualPath)
		{
			UserName = GetParameter("username");
			Password = GetParameter("password");
			Website = GetParameter("website");
			From = GetParameter("from");
			To = GetParameter("to");
			FromName = GetParameter("fromname");
			ToName = GetParameter("toname");
			SmtpServer = GetParameter("smtpserver");
		}
		public string UserName;	// "mail2@jordanstevens.com"
		public string Password;
		public string Website;
		public string From;		// "mail2@jordanstevens.com"
		public string To;
		public string FromName;
		public string ToName;
		public string SmtpServer; // "smtpout.secureserver.net"
		public override void Save()
		{
			Clear();
			m_parameters.Add("username=" + UserName);
			m_parameters.Add("password=" + Password);
			m_parameters.Add("website=" + Website);
			m_parameters.Add("from=" + From);
			m_parameters.Add("to=" + To);
			m_parameters.Add("fromname=" + FromName);
			m_parameters.Add("toname=" + ToName);
			m_parameters.Add("smtpserver=" + SmtpServer);
			base.Save();
		}
	}
	public static MailSettings SystemMailSettings
	{
		get
		{
			if(m_mailSettings == null)
				m_mailSettings = new MailSettings("~/App_Data/mail.txt");
			return m_mailSettings;
		}
	}
	private static MailSettings m_mailSettings = null;
	public class LogEmailSubscribers : Settings
	{
		public LogEmailSubscribers(string virtualPath)
			: base(virtualPath)
		{
			Subscribers = new List<Subscriber>();
			for (int i = 0; i < m_parameters.Count; ++i)
			{
				if (!String.IsNullOrEmpty(m_parameters[i]))
				{
					Subscriber subscriber = new Subscriber();
					var split = m_parameters[i].Split('=');
					subscriber.Email = split[0];
					subscriber.Logs = split[1].Contains("log");
					subscriber.Errs = split[1].Contains("err");
					subscriber.Dmps = split[1].Contains("dmp");
					Subscribers.Add(subscriber);
				}
			}
		}
		public List<Subscriber> Subscribers;
		public class Subscriber
		{
			public string Email;	// "mail2@jordanstevens.com"
			public bool Logs;
			public bool Errs;
			public bool Dmps;

		}
		public void Add(string email, bool logs, bool errors, bool dumps)
		{
			var subscriber = new Subscriber();
			subscriber.Email = email;
			subscriber.Logs = logs;
			subscriber.Errs = errors;
			subscriber.Dmps = dumps;
			Subscribers.Add(subscriber);
		}
		public override void Clear()
		{
			Subscribers = new List<Subscriber>();
			base.Clear();
		}
		public override void Save()
		{
			foreach (var subscriber in Subscribers)
			{
				m_parameters.Add(subscriber.Email + "=" + (subscriber.Logs ? "log|" : "") + (subscriber.Errs ? "err|" : "") + (subscriber.Dmps ? "dmp|" : ""));
			}
			base.Save();
		}
	}
	public static LogEmailSubscribers LogSubscribers
	{
		get
		{
			if (m_logSubscribers == null)
				m_logSubscribers = new LogEmailSubscribers("~/logs/subscribers.txt");
			return m_logSubscribers;
		}
	}
	public static LogEmailSubscribers m_logSubscribers = null;

	/// <summary>
	/// Helper class for the products.
	/// </summary>
	public class Product
	{
		/// <summary>
		/// Product name with underscore
		/// </summary>
		/// <param name="ProductName"></param>
		public Product(string ProductName)
		{
			m_name = ProductName;
			m_productDirectory = Path.Combine(ProductBaseDirectory, ProductName);
		}

		private string m_productDirectory;
		private string m_name;
		public LogEmailSubscribers LogSubscribers
		{
			get
			{
				if (m_logSubscribers == null)
					m_logSubscribers = new LogEmailSubscribers("~/products/" + m_name + "/logs/subscribers.txt");
				return m_logSubscribers;
			}
		}
		public LogEmailSubscribers m_logSubscribers = null;
		public string Name
		{
			get
			{
				return m_name;
			}
		}
		/// <summary>
		/// Gets the product names with underscores instead of spaces.
		/// </summary>
		/// <returns></returns>
		public static Product[] GetProducts()
		{
			var directories = Directory.EnumerateDirectories(ProductBaseDirectory);
			List<Product> products = new List<Product>();
			foreach (var directory in directories)
			{
				var name = directory.Substring(directory.LastIndexOf(Path.DirectorySeparatorChar) + 1);
				products.Add(new Product(name));
			}
			return products.ToArray();
		}
		public static string ProductBaseDirectory
		{
			get
			{
				if (m_productBaseDirectory == null)
					m_productBaseDirectory = HttpContext.Current.Server.MapPath("~/products/");
				return m_productBaseDirectory;
			}
		}
		private static string m_productBaseDirectory;
		public string LogBaseDirectory
		{
			get
			{
				if (m_logBaseDirectory == null)
					m_logBaseDirectory = HttpContext.Current.Server.MapPath("~/products/"+m_name+"/logs/");
				return m_logBaseDirectory;
			}
		}
		private string m_logBaseDirectory;
		public string LogSentDirectory
		{
			get
			{
				if (m_logSentDirectory == null)
					m_logSentDirectory = HttpContext.Current.Server.MapPath("~/products/" + m_name + "/logs/sent/");
				return m_logSentDirectory;
			}
		}
		private string m_logSentDirectory;
		public override string ToString()
		{
			return m_name;
		}
		public void WriteLogFile( string data, string extension)
		{
			string logFile = Path.Combine(LogBaseDirectory,DateTime.Now.ToString("yyyyMMdd_")+data.GetHashCode().ToString("X8")+"."+extension);
			File.AppendAllText(logFile, data);
		}
	}
}